def can_spell_with_chars(word, char_counts):
    word_counts = {}
    for char in word:
        if char in word_counts:
            word_counts[char] += 1
        else:
            word_counts[char] = 1
    
    for char, count in word_counts.items():
        if char not in char_counts or count > char_counts[char]:
            return False
    return True

def sum_of_spellable_words(words, chars):
    # 统计字母表中的字母频率
    char_counts = {}
    for char in chars:
        if char in char_counts:
            char_counts[char] += 1
        else:
            char_counts[char] = 1
    
    # 初始化长度之和
    total_length = 0
    
    # 遍历词汇表
    for word in words:
        # 检查单词是否可以用字母表拼写出来
        if can_spell_with_chars(word, char_counts):
            # 如果可以拼写，则累加单词长度
            total_length += len(word)
    
    # 返回长度之和
    return total_length

# 测试例子
words1 = ["cat","bt","hat","tree"]
chars1 = "atach"
print(sum_of_spellable_words(words1, chars1))  # 输出：6

words2 = ["hello","world","leetcode"]
chars2 = "welldonehoneyr"
print(sum_of_spellable_words(words2, chars2))  # 输出：10